require 'rubygems'
require 'open4'
require 'drb'
class SharedFile
  include DRb::DRbUndumped #this fixes DRb's problems unmarshalling these objects.
  attr_accessor :path, :tags, :content
  def initialize(path, tags)
    @path = path
    @tags = tags
    @content = SharedFile.fetch_content(@path)
  end

  def self.fetch_content(path)
    #making this static made it really easy to mock :(
    content = nil
    File.open(path) { |f| content = f.readlines }
    content
  end

  def content=(content)
    File.open(path, "w"){ |f| f.puts(content) }
  end

  def execute
    #TODO could do lots here.  handle interactive, spawn subprocess to improve responsiveness.
    pid, stdin, stdout, stderr = Open4::popen4(@path)
    {:pid => pid, :stdin => stdin, :stdout => stdout, :stderr => stderr}
  end

end
